package com.api.hg.scheld;

import com.api.hg.model.swException;
import com.api.hg.service.SwExceptionBaseService;
import com.api.sw.duban.annotation.Field;
import com.api.sw.duban.util.LogTool;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//com.api.hg.scheld.SwExceptionConjob
/*
    在库：0
    调拨确认中：5
    已领用：1
    借用确认中：6
    已借用：3
    报废确认中：2
    已减账：7
    转出确认中：4
    已转出：8
 */
public class SwExceptionConjob extends BaseCronJob {
    private final static LogTool log = new LogTool("/log/assetlog/luolf/SwExceptionConjob",false);
    @Override
    public void execute() {
        //bengin conjob\
        synchronized (SwExceptionConjob.class){
            compareSWToGB();
            compareSWToGB_vali();
            checkDelete();
        }
    }

    public void compareSWToGB(){
        //iniDate();
        int max=10;//
        SwExceptionBaseService SwExceptionBaseService=new SwExceptionBaseService();
        RecordSet re=new RecordSet();
        //String  sql=" select* from uf_swzczmxx a  where len(isnull(kpwybh,'')+isnull(kph,''))>0 and " +
                //" not exists (select 1  from uf_gbxtzczmxx b where ((b.kpwybs=a.kpwybh or b.kph=a.kpwybh ) and len(isnull(b.kpwybs,''))>0) or ((b.kpwybs=a.kph or b.kph=a.kph ) and len(isnull(b.kph,''))>0) ) ";
        //String  sql=" select* from uf_swzczmxx a  where  " +
               // " not exists (select 1  from uf_gbxtzczmxx b where ((b.kpwybs=a.kpwybh or b.kph=a.kpwybh ) and len(isnull(b.kpwybs,''))>0) or ((b.kpwybs=a.kph or b.kph=a.kph ) and len(isnull(b.kph,''))>0) ) ";
        //去掉 zczt为7 已减帐
        String sql=" select * from uf_swzczmxx a  where   isnull(a.zczt,-1) in (0,1,2,4,5) and len(isnull(a.xzgbxtzc,''))=0 and \n" +
                   " not exists (select 1  from uf_gbxtzczmxx b where ((b.kpwybs=a.kpwybh or b.kpwybs=a.xzgbxtzc ) and len(isnull(b.kpwybs,''))>0) \n" +
                "and len( isnull(b.sxrq,''))>0 and isnull(b.kpzt,-1) in (0,1,2,3)  ) \n";
        log.writeLog(sql);
        re.execute(sql);
        List<Object> business =new ArrayList<Object>() ;
        swException swException=null;
        while(re.next()){
            swException=new swException();
            String swzcid= Util.null2String(re.getString("id"));;//实物资产ID
            String kpwybh= Util.null2String(re.getString("kpwybh"));//卡片唯一编号
            String kph=Util.null2String(re.getString("kph"));//卡片号
            String swzcbh=Util.null2String(re.getString("swzcbh"));//实物资产编号
            String zcmc=Util.null2String(re.getString("zcmc"));//	资产名称
            String ggxh=Util.null2String(re.getString("ggxh"));//	规格型号
            String pp=Util.null2String(re.getString("pp"));//	品牌
            float dj=Util.getFloatValue(re.getString("dj"),0);//	单价
            String zczt=Util.null2String(re.getString("zczt"));;//资产状态
            String rkrq=Util.null2String(re.getString("rkrq"));;//	入库日期
            int synx=Util.getIntValue(re.getString("synx"),0);//使用年限
            int syks=Util.getIntValue(re.getString("syks"),0);//	使用科室
            int zrr=Util.getIntValue(re.getString("zrr"),0);//责任人
            int dybm=Util.getIntValue(re.getString("dybm"),0);//对应部门(权限用)
            String cqzk=Util.null2String(re.getString("cqzk"));;//	产权状况
            String macdz=Util.null2String(re.getString("macdz"));;//MAC地址
            String jqm=Util.null2String(re.getString("jqm"));;//	机器名
            int zclx=Util.getIntValue(re.getString("zclx"),-1);;//	机器名
            swException.setSwzcid(swzcid);
            //swException.setSwzcid(kpwybh);
            swException.setKpwybh(kpwybh);
            swException.setKph(kph);
            swException.setSwzcbh(swzcbh);
            swException.setZcmc(zcmc);
            swException.setGgxh(ggxh);
            swException.setPp(pp);
            swException.setDj(dj);
            swException.setZczt(zczt);
            swException.setRkrq(rkrq);
            swException.setSynx(synx);
            swException.setSyks(syks);
            swException.setZrr(zrr);
            swException.setDybm(dybm);
            swException.setCqzk(cqzk);
            swException.setMacdz(macdz);
            swException.setJqm(jqm);
            swException.setZclx(zclx);
            if(checkVali(swzcid)){//存在
             // if(checkVali(kpwybh)){//存在
                continue;
            }
            //swException.setSfpk(1);
            business.add(swException);
            //if(business.size()==max){
                //开始写入
                String mess=SwExceptionBaseService.getObjMapList(business,null);
                if(!"".equals(mess)){
                    log.writeLog(business.toString());
                }
                //清空business
                business.clear();
            //}
        }
        if(business.size()>0){
            String mess=SwExceptionBaseService.getObjMapList(business,null);
            if(!"".equals(mess)){
                log.writeLog(business.toString());
            }
        }
        updGbDate();
        setQx();
    }
    /*
        存在
     */
    public void compareSWToGB_vali(){
        iniData();
        int max=10;//
        List<Map<String, Object>> listMap=new ArrayList<Map<String, Object>>();
        SwExceptionBaseService SwExceptionBaseService=new SwExceptionBaseService();
        RecordSet re=new RecordSet();
        //String  sql=" select* from uf_swzczmxx a  where len(isnull(kpwybh,'')+isnull(kph,''))>0 and " +
        //" not exists (select 1  from uf_gbxtzczmxx b where ((b.kpwybs=a.kpwybh or b.kph=a.kpwybh ) and len(isnull(b.kpwybs,''))>0) or ((b.kpwybs=a.kph or b.kph=a.kph ) and len(isnull(b.kph,''))>0) ) ";
        String  sql=" select* from uf_swzczmxx a  where  \n" +
                "  exists (select 1  from uf_gbxtzczmxx b where ((b.kpwybs=a.kpwybh or b.dyswzc=a.id ) and len(isnull(b.kpwybs,''))>0) ) \n";
                //"  exists (select 1  from uf_gbxtzczmxx b where ((b.kpwybs=a.kpwybh or b.kph=a.kpwybh or b.dyswzc=a.id ) and len(isnull(b.kpwybs,''))>0) or ((b.kpwybs=a.kph or b.kph=a.kph ) and len(isnull(b.kph,''))>0) ) ";
        log.writeLog(sql);
        re.execute(sql);
        List<Object> business =new ArrayList<Object>() ;
        swException swException=null;
        while(re.next()){
            swException=new swException();
            String swzcid= Util.null2String(re.getString("id"));;//实物资产ID
            String kpwybh= Util.null2String(re.getString("kpwybh"));//卡片唯一编号
            String kph=Util.null2String(re.getString("kph"));//卡片号
            String swzcbh=Util.null2String(re.getString("swzcbh"));//实物资产编号
            String zcmc=Util.null2String(re.getString("zcmc"));//	资产名称
            String ggxh=Util.null2String(re.getString("ggxh"));//	规格型号
            String pp=Util.null2String(re.getString("pp"));//	品牌
            float dj=Util.getFloatValue(re.getString("dj"),0);//	单价
            String zczt=Util.null2String(re.getString("zczt"));;//资产状态
            String rkrq=Util.null2String(re.getString("rkrq"));;//	入库日期
            int synx=Util.getIntValue(re.getString("synx"),0);//使用年限
            int syks=Util.getIntValue(re.getString("syks"),0);//	使用科室
            int zrr=Util.getIntValue(re.getString("zrr"),0);//责任人
            int dybm=Util.getIntValue(re.getString("dybm"),0);//对应部门(权限用)
            String cqzk=Util.null2String(re.getString("cqzk"));;//	产权状况
            String macdz=Util.null2String(re.getString("macdz"));;//MAC地址
            String jqm=Util.null2String(re.getString("jqm"));;//	机器名
            swException.setSwzcid(swzcid);
            swException.setKpwybh(kpwybh);
            swException.setKph(kph);
            swException.setSwzcbh(swzcbh);
            swException.setZcmc(zcmc);
            swException.setGgxh(ggxh);
            swException.setPp(pp);
            swException.setDj(dj);
            swException.setZczt(zczt);
            swException.setRkrq(rkrq);
            swException.setSynx(synx);
            swException.setSyks(syks);
            swException.setZrr(zrr);
            swException.setDybm(dybm);
            swException.setCqzk(cqzk);
            swException.setMacdz(macdz);
            swException.setJqm(jqm);
            if(checkVali(swzcid)){//存在
                log.writeLog("存在--->"+kph);
                Object[] param1=new Object[2];
                String sql11="update uf_swnewexception  set sfyxz=? where Swzcid=?";
                param1[0]=2;
                param1[1]=swzcid;
                Map<String,Object> sqlmap1=new HashMap<>();
                sqlmap1.put("sql",sql11);
                sqlmap1.put("params",param1);
                listMap.add(sqlmap1);


                String mess=SwExceptionBaseService.getObjMapList(business,listMap);
                if(!"".equals(mess)){
                    log.writeLog(business.toString());
                }
            }
            //swException.setSfpk(1);
            //business.add(swException);
            //if(business.size()==max){
            //开始写入
            listMap.clear();
            //清空business
            business.clear();
            //}
        }
        if(business.size()>0){
            String mess=SwExceptionBaseService.getObjMapList(business,null);
            if(!"".equals(mess)){
                log.writeLog(business.toString());
            }
        }
        updGbDate();
        setQx();
    }
    //更新 关保的数据
    public void updGbDate(){
        RecordSet re=new RecordSet();
        RecordSet re1=new RecordSet();
        String  sql=" select id from uf_gbxtzczmxx a  where len(isnull(kpwybs,'')+isnull(kph,''))>0 and isnull(sybm,0)>0 and " +
                    " exists (select 1  from uf_swzczmxx b where ((b.kpwybh=a.kpwybs or b.kph=a.kpwybs  ) and len(isnull(b.kpwybh,''))>0) or ((b.kpwybh=a.kph or b.kph=a.kph ) and len(isnull(b.kph,''))>0) and b.dybm!=a.sybm   ) ";
        log.writeLog(sql);
        re.execute(sql);
        List<Object> business =new ArrayList<Object>() ;
        swException swException=null;
        while(re.next()){
            //更新数据
            String id=Util.null2String(re.getString("id"));
            re1.execute("update uf_gbxtzczmxx set bmsfyz='1' where id="+id);
        }
    }

    /*
        添加默认共享权限
     */
    public void setQx(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                swException swException=new swException();
                RecordSet re=new RecordSet();
                String sql="select * from uf_swnewexception  ";
                re.execute(sql);
                while(re.next()){
                    String id=Util.null2String(re.getString("id")).trim();
                    ModeRightInfo mri=new ModeRightInfo();
                    mri.editModeDataShare(Util.getIntValue(1+"",0),Util.getIntValue(swException.getFormmodeid(),0),Util.getIntValue(id,0));
                }
            }
        }).start();
    }
    /*
        初始化数据
     */
    public void iniData(){
        RecordSet re=new RecordSet();
        //re.execute("delete from uf_swnewexception");
        re.execute("update uf_gbxtzczmxx set bmsfyz=null");
    }
    /*
        判断是否已经存在
     */
    public boolean  checkVali(String swzcid){
        RecordSet re=new RecordSet();
        //String sql="select *　from　uf_swnewexception　where　kpwybh＝'"+swzcid+"'";
        String sql="select *　from　uf_swnewexception　where　swzcid='"+swzcid+"'";
        re.execute(sql);
        if(re.next()){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 检查实物资产是否已经删除  如果删除则把盘盈异常表里的记录删除
     */
    public  void checkDelete(){
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        rs1.execute("select swzcid,swzcbh from uf_swnewexception");
        while (rs1.next()){
            String swzcid = rs1.getString("swzcid");
            String swzcbh = rs1.getString("swzcbh");
            rs2.execute("select id from uf_swzczmxx where id="+swzcid);
            if(!rs2.next()){
                rs3.execute("delete from uf_swnewexception where swzcid="+swzcid);
                log.writeLog("checkDelete-->实物id"+swzcid+"不存在实物表中,删除实物异常表数据");
            }
        }
        //2024年9月18日 解决实物对碰盘盈表中使用部门在实物资产领用后未更新的情况
        rs3.executeUpdate("update uf_swnewexception set dybm=uf_swzczmxx.dybm from uf_swzczmxx where uf_swnewexception.swzcid=uf_swzczmxx.id");
    }

}
